/**
 *  Refer to Spring JPetStore
 */
package cn.fanyamin.web.struts;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;


/**
 * DOCUMENT ME!
 *
 * @author $author$
 * @version $Revision: 1.1.1.1 $
  */
public class BaseActionForm extends ActionForm {
    /* Public Methods */
    /**
     * DOCUMENT ME!
     *
     * @param mapping DOCUMENT ME!
     * @param request DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors actionErrors = null;
        ArrayList errorList = new ArrayList();
        doValidate(mapping, request, errorList);
        request.setAttribute("errors", errorList);

        if (!errorList.isEmpty()) {
            actionErrors = new ActionErrors();
            actionErrors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("global.error"));
        }

        return actionErrors;
    }

    /**
     * DOCUMENT ME!
     *
     * @param mapping DOCUMENT ME!
     * @param request DOCUMENT ME!
     * @param errors DOCUMENT ME!
     */
    public void doValidate(ActionMapping mapping, HttpServletRequest request, List errors) {
    }

    /* Protected Methods */
    /**
     * DOCUMENT ME!
     *
     * @param errors DOCUMENT ME!
     * @param message DOCUMENT ME!
     * @param value DOCUMENT ME!
     */
    protected void addErrorIfStringEmpty(List< String > errors, String message, String value) {
        if ((value == null) || (value.trim().length() <  1)) {
            errors.add(message);
        }
    }
}
